package com.ruoyi.system.controller;

import com.fasterxml.jackson.databind.JsonNode;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.system.api.RemoteModelService;
import com.ruoyi.system.api.RemoteUserService;
import com.ruoyi.system.api.model.ModelRequest;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.IDataAwardService;
import com.ruoyi.system.service.IDataModelService;
import com.ruoyi.system.service.IDataPaperService;
import com.ruoyi.system.service.IDataPatentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



@RestController
@RequestMapping("/headPaper")
public class HeadPageController extends BaseController {

    @Autowired
    private IDataAwardService dataAwardService;

    @Autowired
    private IDataPaperService dataPaperService;

    @Autowired
    private IDataPatentService dataPatentService;

    @Autowired
    private IDataModelService dataModelService;

    @Autowired
    private RemoteModelService remoteModelService;

    /**
     * 查询论文总数、专利总数、奖项总数、当前年份
     */
//    @RequiresPermissions("system:patent:query")
    @GetMapping(value = "/getInfo")
    public AjaxResult getInfo()
    {
        // 获取当前年份
        int currentYear = LocalDate.now().getYear();

        // 调用服务方法获取各表的记录总数
        long patentNum = dataPatentService.selectDataPatentList(new DataPatent()).size();
        long paperNum = dataPaperService.selectDataPaperList(new DataPaper()).size();
        long awardNum = dataAwardService.selectDataAwardList(new DataAward()).size();

        // 封装响应数据
        Map<String, Object> result = new HashMap<>();
        result.put("patentNum", patentNum);
        result.put("paperNum", paperNum);
        result.put("awardNum", awardNum);
        result.put("currentYear", currentYear);

        return success(result);
    }

    /**
     * 查寻数据库中的模型列表
     */
//    @RequiresPermissions("system:patent:query")
    @GetMapping(value = "/getModelList")
    public TableDataInfo getModelList()
    {
        startPage();
        List<Map<String,Object>> modelList = new ArrayList<>();

        Map<String,Object> NERMap = new HashMap<>();

        DataModel dataModel = new DataModel();

        NERMap.put("taskName","实体要素抽取");
        NERMap.put("taskEnName","NER");
        dataModel.setModelType(1L);
        NERMap.put("modelList",dataModelService.selectDataModelListByType(dataModel));

        Map<String,Object> REMap = new HashMap<>();
        REMap.put("taskName","关系要素抽取");
        REMap.put("taskEnName","RE");
        dataModel.setModelType(2L);
        REMap.put("modelList",dataModelService.selectDataModelListByType(dataModel));

        Map<String,Object> EEMap = new HashMap<>();
        EEMap.put("taskName","事件要素抽取");
        EEMap.put("taskEnName","EE");
        dataModel.setModelType(3L);
        EEMap.put("modelList",dataModelService.selectDataModelListByType(dataModel));

        modelList.add(NERMap);
        modelList.add(REMap);
        modelList.add(EEMap);

        return getDataTable(modelList);
    }

    @PostMapping(value = "/getModelResult")
    public R<List<Map<String, Object>>> getModelResult(@RequestBody ModelRequest modelRequest)
    {
        R<List<Map<String, Object>>> result;

        switch (modelRequest.getTaskName()) {
            case "实体要素抽取":
                result = remoteModelService.getNERResult(modelRequest, SecurityConstants.INNER);
                break;
            case "关系要素抽取":
                result = remoteModelService.getREResult(modelRequest, SecurityConstants.INNER);
                break;
            case "事件要素抽取":
                result = remoteModelService.getEEResult(modelRequest, SecurityConstants.INNER);
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + modelRequest.getTaskName());
        }

        return result;
    }
}
